{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# 拼接和堆叠"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-04-25T14:59:32.225495400Z",
     "start_time": "2024-04-25T14:59:32.187565500Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "沿轴 0 连接两个数组：\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]\n",
      " [7 8]]\n",
      "--------------------------------------------------\n",
      "沿轴 1 连接两个数组：\n",
      "[[1 2 5 6]\n",
      " [3 4 7 8]]\n",
      "--------------------------------------------------\n",
      "[[[1 2]\n",
      "  [3 4]]\n",
      "\n",
      " [[5 6]\n",
      "  [7 8]]]\n"
     ]
    }
   ],
   "source": [
    "# 1. concatenate()函数\n",
    "import numpy as np\n",
    "\n",
    "# 1. 根据轴连接的数组序列，concatenate没有改变数组的维度\n",
    "a = np.array([[1, 2], [3, 4]])\n",
    "b = np.array([[5, 6], [7, 8]])\n",
    "\n",
    "# 要求a,b两个数组的维度相同\n",
    "print('沿轴 0 连接两个数组：')\n",
    "print(np.concatenate((a, b), axis=0))\n",
    "# print(np.append(a,b,axis=0))\n",
    "print('-' * 50)\n",
    "print('沿轴 1 连接两个数组：')\n",
    "print(np.concatenate([a, b], axis=1))\n",
    "print('-' * 50)\n",
    "print(np.stack([a, b], axis=0))  #stack会增加维度\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n",
      "<class 'list'>\n",
      "沿轴 0 堆叠两个数组：\n",
      "(10, 3, 4)\n",
      "[[[ 0  1  2  3]\n",
      "  [ 4  5  6  7]\n",
      "  [ 8  9 10 11]]\n",
      "\n",
      " [[ 0  1  2  3]\n",
      "  [ 4  5  6  7]\n",
      "  [ 8  9 10 11]]\n",
      "\n",
      " [[ 0  1  2  3]\n",
      "  [ 4  5  6  7]\n",
      "  [ 8  9 10 11]]\n",
      "\n",
      " [[ 0  1  2  3]\n",
      "  [ 4  5  6  7]\n",
      "  [ 8  9 10 11]]\n",
      "\n",
      " [[ 0  1  2  3]\n",
      "  [ 4  5  6  7]\n",
      "  [ 8  9 10 11]]\n",
      "\n",
      " [[ 0  1  2  3]\n",
      "  [ 4  5  6  7]\n",
      "  [ 8  9 10 11]]\n",
      "\n",
      " [[ 0  1  2  3]\n",
      "  [ 4  5  6  7]\n",
      "  [ 8  9 10 11]]\n",
      "\n",
      " [[ 0  1  2  3]\n",
      "  [ 4  5  6  7]\n",
      "  [ 8  9 10 11]]\n",
      "\n",
      " [[ 0  1  2  3]\n",
      "  [ 4  5  6  7]\n",
      "  [ 8  9 10 11]]\n",
      "\n",
      " [[ 0  1  2  3]\n",
      "  [ 4  5  6  7]\n",
      "  [ 8  9 10 11]]]\n"
     ]
    }
   ],
   "source": [
    "# 2. 根据轴进行堆叠，2维会变为3维\n",
    "arrays = [np.arange(12).reshape(3, 4) for _ in range(10)]  # 包含10个3x4的数组的列表\n",
    "print(len(arrays))\n",
    "print('沿轴 0 堆叠两个数组：')\n",
    "result0 = np.stack(arrays, axis=0)\n",
    "print(result0.shape)  #(10,3,4)\n",
    "print(result0)\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-04-25T15:14:29.144162500Z",
     "start_time": "2024-04-25T15:14:29.110074500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "沿轴 1 堆叠两个数组：\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "(3, 10, 4)\n",
      "[[[ 0  1  2  3]\n",
      "  [ 0  1  2  3]\n",
      "  [ 0  1  2  3]\n",
      "  [ 0  1  2  3]\n",
      "  [ 0  1  2  3]\n",
      "  [ 0  1  2  3]\n",
      "  [ 0  1  2  3]\n",
      "  [ 0  1  2  3]\n",
      "  [ 0  1  2  3]\n",
      "  [ 0  1  2  3]]\n",
      "\n",
      " [[ 4  5  6  7]\n",
      "  [ 4  5  6  7]\n",
      "  [ 4  5  6  7]\n",
      "  [ 4  5  6  7]\n",
      "  [ 4  5  6  7]\n",
      "  [ 4  5  6  7]\n",
      "  [ 4  5  6  7]\n",
      "  [ 4  5  6  7]\n",
      "  [ 4  5  6  7]\n",
      "  [ 4  5  6  7]]\n",
      "\n",
      " [[ 8  9 10 11]\n",
      "  [ 8  9 10 11]\n",
      "  [ 8  9 10 11]\n",
      "  [ 8  9 10 11]\n",
      "  [ 8  9 10 11]\n",
      "  [ 8  9 10 11]\n",
      "  [ 8  9 10 11]\n",
      "  [ 8  9 10 11]\n",
      "  [ 8  9 10 11]\n",
      "  [ 8  9 10 11]]]\n"
     ]
    }
   ],
   "source": [
    "print('沿轴 1 堆叠两个数组：')\n",
    "print(arrays[0])\n",
    "result1 = np.stack(arrays, axis=1)\n",
    "print(result1.shape)  #(3,10,4)\n",
    "print(result1)\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-04-25T15:02:08.289288500Z",
     "start_time": "2024-04-25T15:02:08.266655700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "沿轴 2连接两个数组：这里-1和2是等价的\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "(3, 4, 10)\n",
      "[[[ 0  0  0  0  0  0  0  0  0  0]\n",
      "  [ 1  1  1  1  1  1  1  1  1  1]\n",
      "  [ 2  2  2  2  2  2  2  2  2  2]\n",
      "  [ 3  3  3  3  3  3  3  3  3  3]]\n",
      "\n",
      " [[ 4  4  4  4  4  4  4  4  4  4]\n",
      "  [ 5  5  5  5  5  5  5  5  5  5]\n",
      "  [ 6  6  6  6  6  6  6  6  6  6]\n",
      "  [ 7  7  7  7  7  7  7  7  7  7]]\n",
      "\n",
      " [[ 8  8  8  8  8  8  8  8  8  8]\n",
      "  [ 9  9  9  9  9  9  9  9  9  9]\n",
      "  [10 10 10 10 10 10 10 10 10 10]\n",
      "  [11 11 11 11 11 11 11 11 11 11]]]\n"
     ]
    }
   ],
   "source": [
    "print('沿轴 2连接两个数组：这里-1和2是等价的')\n",
    "result2 = np.stack(arrays, axis=-1)\n",
    "print(arrays[0])\n",
    "print(result2.shape)  # (3,4,10)\n",
    "print(result2)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-04-25T15:07:24.418459200Z",
     "start_time": "2024-04-25T15:07:24.386547800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "--------------------------------------------------\n",
      "[[ 0  1  2  3  4  5 12 13 14 15 16 17]\n",
      " [ 6  7  8  9 10 11 18 19 20 21 22 23]]\n"
     ]
    }
   ],
   "source": [
    "# 3.矩阵垂直拼接，vstack没有增加维数，类似于concatenate\n",
    "v1 = [[0, 1, 2, 3, 4, 5],\n",
    "      [6, 7, 8, 9, 10, 11]]\n",
    "v2 = [[12, 13, 14, 15, 16, 17],\n",
    "      [18, 19, 20, 21, 22, 23]]\n",
    "result = np.vstack((v1, v2))\n",
    "print(result)\n",
    "print(\"-\" * 50)\n",
    "\n",
    "# 4. 矩阵水平拼接，类似于concatenate\n",
    "v1 = [[0, 1, 2, 3, 4, 5],\n",
    "      [6, 7, 8, 9, 10, 11]]\n",
    "v2 = [[12, 13, 14, 15, 16, 17],\n",
    "      [18, 19, 20, 21, 22, 23]]\n",
    "result = np.hstack((v1, v2))\n",
    "print(result)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-04-25T15:16:16.138238700Z",
     "start_time": "2024-04-25T15:16:16.118294100Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 分割数组"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 1 2]\n",
      " [3 4 5]\n",
      " [6 7 8]]\n",
      "--------------------------------------------------\n",
      "将数组分为三个大小相等的子数组：\n",
      "[array([[0],\n",
      "       [3],\n",
      "       [6]]), array([[1],\n",
      "       [4],\n",
      "       [7]]), array([[2],\n",
      "       [5],\n",
      "       [8]])]\n",
      "(3, 1)\n",
      "--------------------------------------------------\n",
      "[array([[0],\n",
      "       [3],\n",
      "       [6]]), array([[1],\n",
      "       [4],\n",
      "       [7]]), array([[2],\n",
      "       [5],\n",
      "       [8]])]\n",
      "(3, 1)\n"
     ]
    }
   ],
   "source": [
    "# 1. 将一个数组分割为多个子数组\n",
    "# 返回一个列表，包含切分后的子数组\n",
    "'''\n",
    "参数说明：\n",
    "ary：被分割的数组\n",
    "indices_or_sections：是一个整数，就用该数平均切分，如果是一个数组，为沿轴切分的位置（左开右闭）\n",
    "axis：沿着哪个维度进行切向，默认为0，横向切分。为1时，纵向切分\n",
    "'''\n",
    "import numpy as np\n",
    "\n",
    "arr = np.arange(9).reshape(3, 3)\n",
    "print(arr)\n",
    "print(\"-\" * 50)\n",
    "\n",
    "print('将数组分为三个大小相等的子数组：')\n",
    "b = np.split(arr, 3)\n",
    "print(b)\n",
    "print(b[0].shape)\n",
    "print(\"-\" * 50)\n",
    "\n",
    "c = np.hsplit(arr, 3)\n",
    "print(c)\n",
    "print(c[0].shape)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-04-25T15:27:32.326943600Z",
     "start_time": "2024-04-25T15:27:32.316443Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.18084862 0.85294921 0.84935392 0.64615126 0.83343141 0.469802  ]\n",
      " [0.10777218 0.46301816 0.62543095 0.03570199 0.05878046 0.11732039]]\n",
      "--------------------------------------------------\n",
      "原array：\n",
      "[[1. 8. 3. 4. 4. 1.]\n",
      " [3. 6. 3. 1. 6. 8.]]\n",
      "拆分后：\n",
      "[array([[1., 8.],\n",
      "       [3., 6.]]), array([[3., 4.],\n",
      "       [3., 1.]]), array([[4., 1.],\n",
      "       [6., 8.]])]\n",
      "--------------------------------------------------\n",
      "原array：\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]\n",
      " [12 13 14 15]]\n",
      "拆分后：\n",
      "[array([[0, 1, 2, 3],\n",
      "       [4, 5, 6, 7]]), array([[ 8,  9, 10, 11],\n",
      "       [12, 13, 14, 15]])]\n"
     ]
    }
   ],
   "source": [
    "d = np.random.random((2, 6))\n",
    "print(d)\n",
    "print(\"-\" * 50)\n",
    "\n",
    "harr = np.floor(10 * np.random.random((2, 6)))  # np.floor向下取整\n",
    "print('原array：')\n",
    "print(harr)\n",
    "print('拆分后：')\n",
    "print(np.hsplit(harr, 3))\n",
    "print(\"-\" * 50)\n",
    "\n",
    "print('原array：')\n",
    "a = np.arange(16).reshape(4, 4)\n",
    "print(a)\n",
    "print('拆分后：')\n",
    "b = np.vsplit(a, 2)\n",
    "print(b)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-04-25T15:37:07.603130700Z",
     "start_time": "2024-04-25T15:37:07.583189400Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# numpy inf和nan"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "nan <class 'float'>\n",
      "inf <class 'float'>\n"
     ]
    }
   ],
   "source": [
    "'''\n",
    "C 语言中表示最大的正整数值是 0x7FFF FFFF，最小的负整数是 0x8000 0000。\n",
    "查阅资料后，发现 inf 表示无穷大，需要使用 float(‘inf’) 函数来转化，那么对应的就有\tfloat('-inf') 表示无穷小了。\n",
    "这样你就可以使用任意数来判断和它的关系了。\n",
    "那什么时候会出现inf呢？ 比如一个数字除以0，Python中会报错，但是numpy中会是一个inf或者-inf\n",
    "另外还有 nan，这种写法在 pandans 中常见，表示缺失的数据（not a number），所以一般用 nan 来表示。任何与其做运算结果都是 nan\n",
    "'''\n",
    "import numpy as np\n",
    "\n",
    "# 创建一个nan和inf #\n",
    "a = np.nan\n",
    "b = np.inf\n",
    "print(a, type(a))\n",
    "print(b, type(b))\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-04-25T15:54:35.482596700Z",
     "start_time": "2024-04-25T15:54:35.466640Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "True\n",
      "True\n",
      "nan\n"
     ]
    }
   ],
   "source": [
    "# 并 且  np.nan != np.nan\t结果是TRUE\n",
    "# 所以我们可以使用这两个结合使用判断nan的个数\n",
    "print(np.nan == np.nan)\n",
    "print(True == 1)\n",
    "print(False == 0)\n",
    "print(np.nan + 1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-04-25T16:04:21.286316900Z",
     "start_time": "2024-04-25T16:04:21.277595200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8.  9. 10. 11.]\n",
      " [12. 13. 14. 15. nan 17.]\n",
      " [18. 19. 20. 21. nan 23.]]\n",
      "23\n",
      "--------------------------------------------------\n",
      "[[False False False False False False]\n",
      " [False False False False False False]\n",
      " [False False False False  True False]\n",
      " [False False False False  True False]]\n",
      "2\n",
      "--------------------------------------------------\n",
      "[[False False False False False False]\n",
      " [False False False False False False]\n",
      " [False False False False  True False]\n",
      " [False False False False  True False]]\n",
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8.  9. 10. 11.]\n",
      " [12. 13. 14. 15.  0. 17.]\n",
      " [18. 19. 20. 21.  0. 23.]]\n"
     ]
    }
   ],
   "source": [
    "# 判断数组中为nan的个数\n",
    "t = np.arange(24, dtype=float).reshape(4, 6)\n",
    "\n",
    "# 将三行四列的数改成nan\n",
    "t[3, 4] = np.nan\n",
    "t[2, 4] = np.nan\n",
    "print(t)\n",
    "\n",
    "# 可以使用np.count_nonzero() 来判断非零的个数\n",
    "print(np.count_nonzero(t))\n",
    "\n",
    "print('-' * 50)\n",
    "print(t != t)  # 返回一个布尔数组，True表示nan，False表示非nan\n",
    "print(np.count_nonzero(t != t))  # 统计nan的个数\n",
    "print(\"-\"*50)\n",
    "\n",
    "# 将nan替换为0\n",
    "print(np.isnan(t))  # 返回一个布尔数组，True表示nan，False表示非nan\n",
    "t[np.isnan(t)] = 0\n",
    "# 也可以使用 t[t!=t] = 0\n",
    "print(t)\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-04-26T02:40:51.585136200Z",
     "start_time": "2024-04-26T02:40:51.557538300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8. nan nan nan]\n",
      " [12. 13. 14. 15. 16. 17.]\n",
      " [18. 19. 20. 21. 22. 23.]]\n",
      "-------------------\n",
      "(4, 6)\n",
      "2468842274320\n",
      "[ 0.  6. 12. 18.]\n",
      "[ 1.  7. 13. 19.]\n",
      "[ 2.  8. 14. 20.]\n",
      "[ 3. nan 15. 21.]\n",
      "[ 3. 15. 21.]\n",
      "[ 4. nan 16. 22.]\n",
      "[ 4. 16. 22.]\n",
      "[ 5. nan 17. 23.]\n",
      "[ 5. 17. 23.]\n",
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8. 13. 14. 15.]\n",
      " [12. 13. 14. 15. 16. 17.]\n",
      " [18. 19. 20. 21. 22. 23.]]\n",
      "2468842274320\n"
     ]
    }
   ],
   "source": [
    "# 练习： 处理数组中nan\n",
    "t = np.arange(24).reshape(4, 6).astype('float')\n",
    "\n",
    "# 将数组中的一部分替换nan\n",
    "t[1, 3:] = np.nan\n",
    "print(t)\n",
    "print('-------------------')\n",
    "print(t.shape)\n",
    "print(id(t))\n",
    "\n",
    "# 遍历每一列，然后判断每一列是否有nan\n",
    "for i in range(t.shape[1]):\n",
    "    #获取当前列数据\n",
    "    temp_col = t[:, i]\n",
    "    print(temp_col)\n",
    "\n",
    "    # 判断当前列的数据中是否含有nan\n",
    "    nan_num = np.count_nonzero(temp_col != temp_col)\n",
    "    # 条件成立说明含有nan\n",
    "    if nan_num != 0:\n",
    "        # 将这一列不为nan的数据拿出来,并计算平均值\n",
    "        temp_col_not_nan = temp_col[temp_col == temp_col]\n",
    "        print(temp_col_not_nan)\n",
    "        # 将nan替换成这一列的平均值\n",
    "        #temp_col[np.isnan(temp_col)] = np.mean(temp_col_not_nan)\n",
    "        temp_col[temp_col != temp_col] = np.mean(temp_col_not_nan)\n",
    "\n",
    "print(t)\n",
    "print(id(t))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-04-26T02:44:34.565532600Z",
     "start_time": "2024-04-26T02:44:34.552568300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n",
      "False\n"
     ]
    },
    {
     "data": {
      "text/plain": "False"
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(np.inf == np.inf)\n",
    "print(-np.nan == -np.inf)\n",
    "print(np.inf == np.nan)\n",
    "np.inf == -np.inf"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-04-25T16:35:19.807637800Z",
     "start_time": "2024-04-25T16:35:19.795629300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.  2.  4.  6.  8. 10.]\n",
      " [12. 14. 16. nan nan nan]\n",
      " [24. 26. 28. 30. 32. 34.]\n",
      " [36. 38. 40. 42. 44. 46.]]\n"
     ]
    }
   ],
   "source": [
    "# np.nan和任何数据运算的结果都是nan\n",
    "t = np.arange(24).reshape(4, 6).astype('float')\n",
    "\n",
    "# 将数组中的一部分替换nan\n",
    "t[1, 3:] = np.nan\n",
    "\n",
    "t1 = np.arange(24).reshape(4, 6).astype('float')\n",
    "print(t + t1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-04-26T02:45:36.187738200Z",
     "start_time": "2024-04-26T02:45:36.169956900Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-1  0]\n",
      "<class 'numpy.int32'>\n",
      "int32\n",
      "-inf\n",
      "--------------------------------------------------\n",
      "<class 'int'>\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Lxxl\\AppData\\Local\\Temp\\ipykernel_8956\\1187932847.py:5: RuntimeWarning: divide by zero encountered in scalar divide\n",
      "  print(arr[0] / arr[1])  # 1除0就会得到inf\n"
     ]
    },
    {
     "ename": "ZeroDivisionError",
     "evalue": "division by zero",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mZeroDivisionError\u001B[0m                         Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[59], line 12\u001B[0m\n\u001B[0;32m     10\u001B[0m ls \u001B[38;5;241m=\u001B[39m [\u001B[38;5;241m-\u001B[39m\u001B[38;5;241m1\u001B[39m, \u001B[38;5;241m0\u001B[39m]\n\u001B[0;32m     11\u001B[0m \u001B[38;5;28mprint\u001B[39m(\u001B[38;5;28mtype\u001B[39m(ls[\u001B[38;5;241m0\u001B[39m]))\n\u001B[1;32m---> 12\u001B[0m \u001B[38;5;28mprint\u001B[39m(\u001B[43mls\u001B[49m\u001B[43m[\u001B[49m\u001B[38;5;241;43m0\u001B[39;49m\u001B[43m]\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;241;43m/\u001B[39;49m\u001B[43m \u001B[49m\u001B[43mls\u001B[49m\u001B[43m[\u001B[49m\u001B[38;5;241;43m1\u001B[39;49m\u001B[43m]\u001B[49m)\n",
      "\u001B[1;31mZeroDivisionError\u001B[0m: division by zero"
     ]
    }
   ],
   "source": [
    "arr = np.array([-1, 0])\n",
    "print(arr)\n",
    "print(type(arr[0]))\n",
    "print(arr.dtype)\n",
    "print(arr[0] / arr[1])  # 1除0就会得到inf\n",
    "print(\"-\" * 50)\n",
    "\n",
    "# 必须是numpy里的元素去操作才行\n",
    "ls = [-1, 0]\n",
    "print(type(ls[0]))\n",
    "print(ls[0] / ls[1])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-04-25T16:44:28.916194300Z",
     "start_time": "2024-04-25T16:44:28.842802Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 转置和轴滚动"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原数组：\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "\n",
      "对换数组：\n",
      "[[ 0  4  8]\n",
      " [ 1  5  9]\n",
      " [ 2  6 10]\n",
      " [ 3  7 11]]\n",
      "--------------------------------------------------\n",
      "原数组：\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "\n",
      "转置数组：\n",
      "[[ 0  4  8]\n",
      " [ 1  5  9]\n",
      " [ 2  6 10]\n",
      " [ 3  7 11]]\n"
     ]
    }
   ],
   "source": [
    "#对换数组的维度\n",
    "import numpy as np\n",
    "a = np.arange(12).reshape(3,4)\n",
    "print ('原数组：')\n",
    "print (a )\n",
    "print ()\n",
    "print ('对换数组：')\n",
    "print (np.transpose(a)) # 也是开辟新空间，不会改变原数组\n",
    "print('-'*50)\n",
    "\n",
    "# 与transpose一致\n",
    "a = np.arange(12).reshape(3,4)\n",
    "\n",
    "print ('原数组：')\n",
    "print (a)\n",
    "print ()\n",
    "print ('转置数组：')\n",
    "print (a.T)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-04-26T02:53:21.552230200Z",
     "start_time": "2024-04-26T02:53:21.530936200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 原 数 组 ：\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "\n",
      "调用 swapaxes 函数后的数组：\n",
      "[[ 0  6 12 18]\n",
      " [ 1  7 13 19]\n",
      " [ 2  8 14 20]\n",
      " [ 3  9 15 21]\n",
      " [ 4 10 16 22]\n",
      " [ 5 11 17 23]]\n",
      "(6, 4)\n"
     ]
    }
   ],
   "source": [
    "# 函数用于交换数组的两个轴\n",
    "t1 = np.arange(24).reshape(4,6)\n",
    "re1 = t1.swapaxes(1,0) # 交换第0和第1轴\n",
    "\n",
    "print (' 原 数 组 ：')\n",
    "print (t1)\n",
    "print ()\n",
    "print ('调用 swapaxes 函数后的数组：')\n",
    "print (re1)\n",
    "print(re1.shape)\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-04-26T02:54:48.808548200Z",
     "start_time": "2024-04-26T02:54:48.785610Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# swapaxes函数的作用是交换数组的两个轴，并返回交换后的数组，不会改变原数组。\n",
    "# arr.T只能针对二维，swapaxes可以针对任意维度。"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(4, 3, 5)\n",
      "--------------------------------------------------\n",
      "(4, 5, 3)\n"
     ]
    }
   ],
   "source": [
    "t3=np.arange(60).reshape(3,4,5)\n",
    "#t3=np.transpose(t3, (1,0,2)) # (4, 3, 5)\n",
    "print(t3.shape)\n",
    "print('-'*50)\n",
    "t3=np.swapaxes(t3,1,2)\n",
    "#t3 = t3.swapaxes(1,2)\n",
    "print(t3.shape)\n",
    "#print(t3.swapaxes(1,2).shape)\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-04-26T03:03:35.964585200Z",
     "start_time": "2024-04-26T03:03:35.953684700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(5, 3, 4, 6)\n",
      "(3, 4, 5, 6)\n",
      "(3, 6, 4, 5)\n",
      "(3, 6, 4, 5)\n",
      "(3, 4, 5, 6)\n"
     ]
    }
   ],
   "source": [
    "# 数组的轴滚动,swapaxes每次只能交换两个轴，没有rollaxis方便，默认情况下轴滚动最前面\n",
    "a = np.ones((3,4,5,6))\n",
    "print(np.rollaxis(a, 2).shape) # 返回新数组，原数组不变\n",
    "print(a.shape)\n",
    "print(np.rollaxis(a, 3,1).shape)\n",
    "print(np.transpose(a, (0,3,1,2)).shape) # 效果一样\n",
    "print(a.shape)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-04-26T03:15:09.586752400Z",
     "start_time": "2024-04-26T03:15:09.560787Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# copy和赋值的区别"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2468842026544\n",
      "2468821083312\n",
      "2468842026544\n",
      "--------------------------------------------------\n",
      "[[99  2  3]\n",
      " [ 4  5  6]]\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "--------------------------------------------------\n",
      "[[99  2  3]\n",
      " [ 4  5  6]]\n",
      "[[88  2  3]\n",
      " [ 4  5  6]]\n",
      "[[88  2  3]\n",
      " [ 4  5  6]]\n",
      "2468821083312\n",
      "2468842026544\n",
      "2468842026544\n"
     ]
    }
   ],
   "source": [
    "# copy就是完全一个新的数组，id都不同了\n",
    "# 赋值则和原来一样，因为ndarray是可变数据类型，所以改咋变咋变\n",
    "b=np.array([[1,2,3],[4,5,6]])\n",
    "a=b.copy()\n",
    "c=a\n",
    "print(id(a))\n",
    "print(id(b))\n",
    "print(id(c))\n",
    "print('-'*50)\n",
    "\n",
    "b[0,0]=99\n",
    "print(b)\n",
    "print(c)\n",
    "print('-'*50)\n",
    "\n",
    "c[0,0]=88\n",
    "print(b)\n",
    "print(c)\n",
    "print(a)\n",
    "print(id(b))\n",
    "print(id(a))\n",
    "print(id(c))\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-04-26T03:28:16.574346200Z",
     "start_time": "2024-04-26T03:28:16.555606400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
